﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using EZCRS.HCMS.Data;

namespace EZCRS.HCMS.SqlClient
{
    public class sql_AdmCompany:MainBase
    {
        private string SqlStr = "", ErrMsg = "";

        #region 添加或修改公司
        /// <summary>
        /// 添加或修改
        /// </summary>
        /// <param name="CompanyId">企业编号 0 为新增</param>
        /// <param name="GId"></param>
        /// <param name="PId"></param>
        /// <param name="CompanyName"></param>
        /// <param name="CompanyNameE"></param>
        /// <param name="CompanyType"></param>
        /// <param name="AddressCountry"></param>
        /// <param name="AddrProvince"></param>
        /// <param name="AddrCity"></param>
        /// <param name="Address"></param>
        /// <param name="Post"></param>
        /// <param name="CompanyTel"></param>
        /// <param name="CompanyFax"></param>
        /// <param name="CompanyEmail"></param>
        /// <param name="FirstName"></param>
        /// <param name="LastName"></param>
        /// <param name="Gender"></param>
        /// <param name="Tel"></param>
        /// <param name="Email"></param>
        /// <param name="Orate"></param>
        /// <param name="Iata"></param>
        /// <param name="Status"></param>
        /// <returns></returns>
        public bool Company_Add(int CompanyId, int GId, int PId, string CompanyName, string CompanyNameE, string CompanyType, string AddressCountry, string AddrProvince, string AddrCity, string Address, string Post, string CompanyTel, string CompanyFax, string CompanyEmail, string FirstName, string LastName, string Gender, string Tel, string Email, string Orate, string Iata, string Status)
        {
            SqlStr = "SELECT top 1 * FROM [hfg_COMPANY]";
            if (CompanyId != 0)
            {
                SqlStr += "WHERE [CORPID] = " + CompanyId;
            }
            DataTable dt = ExecSql.GetDataSet(SqlStr).Tables[0];
            if (dt.Rows.Count > 0)
            {
                if (CompanyId != 0)
                {
                    SqlStr = "UPDATE [hfg_COMPANY] SET [gid] = @GId, [pid] = @PId, [corpname] = @CompanyName, [corpname_e] = @CompanyNameE, [corptype] = @CompanyType, [corpcountry] = @AddressCountry,[corpprovince] = @AddrProvince, [corpcity] = @AddrCity, [corpaddress] = @Address,[corppost] = @Post, [corptel] = @CompanyTel,[corpfax] = @CompanyFax,[corpemail]=@CompanyEmail,[confirstname] = @FirstName, [conlastname] = @LastName, [congender] = @Gender, [contel] = @Tel, [conemail] = @Email, [corporate] = @Orate, [corpiata] = @Iata, [corpstatus] = @Status WHERE [corpid] = " + CompanyId;
                }
                else
                {
                    SqlStr = "INSERT INTO [hfg_COMPANY] (gid,pid,corpname,corpname_e,corptype,corpcountry,corpprovince,corpcity,corpaddress,corppost,corptel,corpfax,corpemail,confirstname,conlastname,congender,contel,conemail,corporate,corpiata,corpstatus)";
                    SqlStr += "values (@GId,@PId,@CompanyName,@CompanyNameE,@CompanyType,@AddressCountry,@AddrProvince,@AddrCity,@Address,@Post,@CompanyTel,@CompanyFax,@CompanyEmail,@FirstName,@LastName,@Gender,@Tel,@Email,@Orate,@Iata,@Status)";
                }
                SqlParameter[] ArrSqlPar = new SqlParameter[21];
                ArrSqlPar[0] = new SqlParameter("@GId", GId);
                ArrSqlPar[1] = new SqlParameter("@PId", PId);
                ArrSqlPar[2] = new SqlParameter("@CompanyName", CompanyName);
                ArrSqlPar[3] = new SqlParameter("@CompanyNameE", CompanyNameE);
                ArrSqlPar[4] = new SqlParameter("@CompanyType", CompanyType);
                ArrSqlPar[5] = new SqlParameter("@AddressCountry", AddressCountry);
                ArrSqlPar[6] = new SqlParameter("@AddrProvince", AddrProvince);
                ArrSqlPar[7] = new SqlParameter("@AddrCity", AddrCity);
                ArrSqlPar[8] = new SqlParameter("@Address", Address);
                ArrSqlPar[9] = new SqlParameter("@Post", Post);
                ArrSqlPar[10] = new SqlParameter("@CompanyTel", CompanyTel);
                ArrSqlPar[11] = new SqlParameter("@CompanyFax", CompanyFax);
                ArrSqlPar[12] = new SqlParameter("@CompanyEmail", CompanyEmail);
                ArrSqlPar[13] = new SqlParameter("@FirstName", FirstName);
                ArrSqlPar[14] = new SqlParameter("@LastName", LastName);
                ArrSqlPar[15] = new SqlParameter("@Gender", Gender);
                ArrSqlPar[16] = new SqlParameter("@Tel", Tel);
                ArrSqlPar[17] = new SqlParameter("@Email", Email);
                ArrSqlPar[18] = new SqlParameter("@Orate", Orate);
                ArrSqlPar[19] = new SqlParameter("@Iata", Iata);
                ArrSqlPar[20] = new SqlParameter("@Status", Status);
                if (ExecSql.SqlExecNoquery(SqlStr, ArrSqlPar) > 0)
                    return true;
                else
                    return false;
            }
            return false;
        }
        #endregion

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        /// <param name="PageSize"></param>
        /// <param name="PageIndex"></param>
        /// <param name="StrWhere"></param>
        /// <returns></returns>
        public DataSet Company_List(int PageSize, int PageIndex, string StrWhere)
        {
            return ExecSql.AddParamFrKeyField("hfg_company", "*", "corpid", "corpid", PageSize, PageIndex, StrWhere);
        }

        /// <summary>
        /// 删除企业
        /// </summary>
        /// <param name="CompanyId">企业编号</param>
        /// <returns></returns>
        public bool Company_Del(string CompanyId)
        {
            if (ExecSql.SqlExecNoquery("delete from hfg_company where corpid=" + CompanyId + "")>0)
                return true;
            else
                return false;
        }

        

        /// <summary>
        /// 获得行业类型数据
        /// </summary>
        /// <returns></returns>
        public DataTable Company_BussType()
        {
            switch (LanguageID)
            {
                case 1:
                    SqlStr = "select typid,typnameen as typename from hfg_base_btype order by typrank";
                    break;
                default:
                    SqlStr = "select typid,typnamecn as typename from hfg_base_btype order by typrank";
                    break;
            }
            return ExecSql.GetDataSet(SqlStr).Tables[0];
        }

        /// <summary>
        /// 根据corpid获取DataRow
        /// </summary>
        /// <param name="corpid"></param>
        /// <returns></returns>
        public DataRow GetCompanyListOne(int corpid)
        {
            return ExecSql.GetDataSet("SELECT * FROM [hfg_COMPANY] WHERE CORPID ='" + corpid.ToString() + "'").Tables[0].Rows[0];
        }

        /// <summary>
        /// 查找[hfg_company_user]表中是否存在此公司
        /// </summary>
        /// <param name="corpid">公司ID</param>
        /// <returns>存在：true 不存在：false</returns>
        public bool CheckCompanyUser(int corpid)
        {
            int count = ExecSql.GetDataSet("SELECT * FROM [hfg_COMPANY_USER] WHERE CORPID='" + corpid.ToString() + "'").Tables[0].Rows.Count;
            bool Result = count > 0 ? true : false;
            return Result;
        }
    }
}
